##
# Copyright (c) 2018-2022, NXOS Development Team
# SPDX-License-Identifier: Apache-2.0
# 
# Contains: Makefile for Source code
# 
# Change Logs:
# Date           Author            Notes
# 2021-9-20      JasonHu           Init
##

#
# include xbuild scripts
#
sinclude ../scripts/xbuild/env.mk

#
# Load default variables.
#
ifeq ($(G),y)
ASFLAGS			:= -g -ggdb -O0 -Wall
CFLAGS			:= -g -ggdb -O0 -Wall
else

# backtrace(BT) need O0
ifeq ($(BT),y)
ASFLAGS			:= -Wall -O0
CFLAGS			:= -Wall -O0
else
ASFLAGS			:= -Wall -O3
CFLAGS			:= -Wall -O3
endif # ifeq ($(BT),y)

endif # ifeq ($(G),y)

LDFLAGS			:= -T $(srctree)/platform/$(PLATFORM)/nxos.ld -nostdlib -static
MCFLAGS			:=

LIBDIRS			:=
LIBS 			:=
INCDIRS			:=
SRCDIRS			:=

CP				:=cp

#
# Override default variables.
#
sinclude $(srctree)/platform/$(PLATFORM)/nxos.mk

#
# Add target triple if use llvm toolchain.
#
ifneq ($(LLVM),)
ifeq ($(strip $(ARCH)), x86)
MCFLAGS 	+= --target=i386-unknown-unknown-elf
else
MCFLAGS 	+= --target=$(strip $(ARCH))-unknown-unknown-elf
endif
endif

#
# Modify default variables.
#
ifeq ($(strip $(ARCH)), arm32)
DEFINES		+=	-D__ARM__
endif
ifeq ($(strip $(ARCH)), aarch64)
DEFINES		+=	-D__ARM64__ -D__AARCH64__
endif
ifeq ($(strip $(ARCH)), riscv64)
DEFINES		+=	-D__RISCV64__
endif
ifeq ($(strip $(ARCH)), riscv32)
DEFINES		+=	-D__RISCV32__
endif
ifeq ($(strip $(ARCH)), amd64)
DEFINES		+=	-D__AMD64__
endif
ifeq ($(strip $(ARCH)), x86)
DEFINES		+=	-D__I386__
endif

DEFINES 	+= -DCONFIG_KERNEL_NXOS

RELEASE 	?=n

# debug info
ifeq ($(RELEASE),n)
ASFLAGS		+=	-g
CFLAGS		+=	-g
endif

ASFLAGS		+=	-ffunction-sections -fdata-sections -ffreestanding -nostdinc -std=gnu99 $(DEFINES)
CFLAGS		+=	-ffunction-sections -fdata-sections -ffreestanding -nostdinc -std=gnu99 $(DEFINES)

# has frame pointer
CFLAGS		+=	-fno-omit-frame-pointer -fno-optimize-sibling-calls
OCFLAGS		+= 	-O binary -R .note -R .comment -S

#
# Add necessary directory for INCDIRS and SRCDIRS.
#
INCDIRS		+=	include
INCDIRS		+=	./
INCDIRS		+=	arch/$(ARCH)/include
INCDIRS		+=	platform/$(PLATFORM)/include

# Source codes
SRCDIRS		+=	arch/$(ARCH)/
SRCDIRS		+=	platform/$(PLATFORM)/
SRCDIRS		+=	init/
SRCDIRS		+=	utils/
SRCDIRS		+=	mm/
SRCDIRS		+=	sched/
SRCDIRS		+=	io/
SRCDIRS		+=	process/
SRCDIRS		+=	kernel/
SRCDIRS		+=	drivers/
SRCDIRS		+=	fs/
SRCDIRS		+=	test/
SRCDIRS		+=	time/
SRCDIRS		+=	ipc/
SRCDIRS		+=	trace/

#
# XBuild variables
#
X_ASFLAGS	:=	$(MCFLAGS) $(ASFLAGS)
X_CFLAGS	:=	$(MCFLAGS) $(CFLAGS)
X_LDFLAGS	:=	$(LDFLAGS)
X_ODFLAGS	:=	$(ODFLAGS)
X_OCFLAGS	:=  $(OCFLAGS)
X_INCDIRS	:=	$(INCDIRS)
SRC			:=	$(SRCDIRS)
NAME		:= platform/$(PLATFORM)/$(NXOS_NAME).elf

#
# Custom cmds for xbuild
# Link OS and copy into platform dir
#

ifeq ($(USE_BUILD_DIR), y)

define CUSTOM_TARGET_CMD
echo [KERNEL] $@; \
$(LD) $(X_LDFLAGS) -o $@ $(X_OBJS); \
$(OC) $(X_OCFLAGS) $@ $(NXOS_NAME).bin; \
$(CP) $@ $(srctree)/platform/$(PLATFORM); \
$(CP) $(NXOS_NAME).bin $(srctree)/platform/$(PLATFORM); \
$(CP) $@ ./
endef # CUSTOM_TARGET_CMD

else

define CUSTOM_TARGET_CMD
echo [KERNEL] $@; \
$(LD) $(X_LDFLAGS) -o $@ $(X_OBJS); \
$(CP) $@ ./
endef # CUSTOM_TARGET_CMD

endif # USE_BUILD_DIR
